using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using TickSharp.Core.Interfaces;
using TickSharp.Core.Model;

namespace TickSharp.Core.Infrastructure.Serializers
{
    public class EntrySerializer : ISerializer<Entry>
    {
        public IEnumerable<Entry> Deserialize(XElement elements)
        {
            return from e in elements.Elements()
            where e.Name.LocalName == "entry"
                select new Entry
                {
                    Id = int.Parse(e.Element("id").Value),
                    UserId = int.Parse(e.Element("user_id").Value),
                    Date = DateTime.Parse(e.Element("date").Value),
                    Hours = float.Parse(e.Element("hours").Value),
                    Notes = e.Element("notes").Value,
                    Billable = bool.Parse(e.Element("billable").Value),
                    ProjectId = int.Parse(e.Element("project_id").Value),
                    TaskId = int.Parse(e.Element("task_id").Value),
                };
        }
    }
}